package events.network.riteh;

import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import events.network.riteh.RequestMethod;
//import events.network.riteh.*;

public class RegisterActivity extends Activity {

	private String name;
	private String surname;
	private String email;
	private String password;
	private String passwordRepeat;
	private String date;
	private EditText mName;
	private EditText mSurname;
	private TextView mDate;
	private EditText mEmail;
	private EditText mPassword;
	private EditText mPasswordRepeat;
	private Button mRegister;
	private Button mDatePick;
	private RestClient client;
	private AlertDialog msg;
	Intent result;
	
	private int day;
	private int month;
	private int year;
	static final int DATE_DIALOG_ID = 0;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        
		msg = new AlertDialog.Builder(this).create();
		result = new Intent();
		result.putExtra("id", "0");
		setResult(0);
		
        mRegister = (Button) findViewById(R.id.buttonRegister);
        mDatePick = (Button) findViewById(R.id.buttonRegisterDateSelect);
        mDate = (TextView) findViewById(R.id.textViewRegisterDate);
        
        mDatePick.setOnClickListener(new View.OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		showDialog(DATE_DIALOG_ID);
        	}
    	});
        
        mRegister.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				registerNewAccount();
			}
		});
        
        // GET CURRENT DATE
        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        
        updateDateDisplay();
	}
	
	private void registerNewAccount() {
		mName = (EditText) findViewById(R.id.editTextRegisterName);
		name = mName.getText().toString();
		
		mSurname = (EditText) findViewById(R.id.editTextRegisterSurname);
		surname = mSurname.getText().toString();
		
		mDate = (TextView) findViewById(R.id.textViewRegisterDate);
		date = mDate.getText().toString();
		
		mPassword = (EditText) findViewById(R.id.editTextRegisterPassword);
		password= mPassword.getText().toString();
		
		mPasswordRepeat = (EditText) findViewById(R.id.editTextRegisterRepeatPassword);
		passwordRepeat = mPasswordRepeat.getText().toString();
		
		mEmail = (EditText) findViewById(R.id.editTextRegisterEmail);
		email = mEmail.getText().toString();
	
		String id = "-1";
		
		// TODO
		// VALIDACIJA PODATAKA - provjera emaila
		// Sredjivanje podataka - Prvo veliko slovo na imenu i prezimenu
		
		
		int tmp=0;
		String message = "Current entries are empty:";
		
		msg.setTitle("Error!");
		if (name.isEmpty())  { message += "\nName"; tmp++; }
		if (surname.isEmpty()){ message += "\nSurname";  tmp++; }
		if (email.isEmpty()){ message += "\nEmail";  tmp++; }
		if (date.isEmpty()){ message += "\nDate";  tmp++; }
		if (password.isEmpty()){ message += "\nPassword";  tmp++; }
		if (passwordRepeat.isEmpty()){ message +="\nPassword repeat";  tmp++; }	
		if (tmp > 0) {
			msg.setMessage(message);
			msg.show();
			return;
		}
		
		if (password.compareTo(passwordRepeat) != 0) {
			msg.setMessage("Passwords dont match!");
			msg.show();
			return;
		}
		
		//////////////////////
		
		client = new RestClient(getString(R.string.serverAddress) + getString(R.string.serverMethodRegister));
		client.AddParam("name", name);
		client.AddParam("surname", surname);
		client.AddParam("date", date);
		client.AddParam("email", email);
		client.AddParam("password", password);
		
		try {
			client.Execute(RequestMethod.GET);
		} catch (Exception e) {
				e.printStackTrace();
		}
		
		try { 
			id = client.getResponse(); 
		} catch (Exception e) { 
			e.printStackTrace(); 
		}
		
		if (id == null) {
			result.putExtra("id","-1");
			setResult(1, result);
		}
		else {
			id = id.substring(0, id.length()-1);
			int numId = Integer.parseInt(id);
			if (numId <= 0) {
				result.putExtra("id", "0");
				setResult(2, result);
			}
			else {
				result.putExtra("id", Integer.toString(numId));
				result.putExtra("email", email);
				result.putExtra("pass", password);
				setResult(3, result);
			}	
		}
		finish();
	}
	
	private void updateDateDisplay() {
        mDate.setText(year + "-" + (month+1) + "-" + day);
	}
	
    private DatePickerDialog.OnDateSetListener mDateSetListener = 
    		new DatePickerDialog.OnDateSetListener() {
    			@Override
                public void onDateSet(DatePicker view, int tmpYear, 
                                      int tmpMonth, int tmpDay) {
                    year = tmpYear;
                    month = tmpMonth;
                    day = tmpDay;
                    updateDateDisplay();
                }
    };
    
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                        mDateSetListener,
                        year, month, day);
        }
        return null;
    }
}
